package domain.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import core.context.ContextHolder;

import process.model.OntoResource;
import process.module.OntoQuery;
import domain.web.model.Autocomplete;
import domain.web.model.ProductView;
import domain.web.model.rq.SearchRq;
import domain.web.service.SearchService;

/**
 * @author tvo35
 * 
 */
@Controller
@RequestMapping("data/search")
public class SearchController {
	
	@Autowired
	private SearchService searchService;
	
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	@ResponseBody
	public SearchRq getSearchRequest() {
		//SearchRq searchRequest = new SearchRq();
		// Get from ContextHolder
		SearchRq searchRequest = ContextHolder.getInstance().getSearchRq();
		return searchRequest;
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	@ResponseBody
	public List<ProductView> search(
			@RequestBody SearchRq searchRq) {
		System.out.println(searchRq.getPrice());
		System.out.println(searchRq.getManufacturer());
		// Store to ContextHolder
		ContextHolder.getInstance().setSearchRq(searchRq);
		return searchService.search(searchRq);
	}
	
	@RequestMapping(value = "/getclass", method = RequestMethod.GET)
	@ResponseBody
	public List<Autocomplete> getAll() {
		OntoQuery ontoQuery = new OntoQuery();
		List<OntoResource> resources = ontoQuery.getAllClasses();
		
		List<Autocomplete> categories = new ArrayList<Autocomplete>();
		for (OntoResource ontoResource : resources) {
			String text = OntoQuery.getClearName(ontoResource.getName());
			categories.add(new Autocomplete(0, text, text));
		}
		return categories;
	}
	
	
	
}

